﻿namespace ClassyBlog.Infrastructure
{
    using System;
    using System.Web.Mvc;

    public class ViewEngineRegistrations
    {
        private readonly ViewEngineCollection viewEngines;

        public ViewEngineRegistrations(ViewEngineCollection viewEngines)
        {
            if (viewEngines == null)
            {
                throw new ArgumentNullException("viewEngines");
            }

            this.viewEngines = viewEngines;
        }

        public virtual void Register()
        {
            var resolver = DependencyResolver.Current;
            Func<string> getTheme = () =>
                resolver.GetService<ICurrentBlogProvider>().Current.Theme;

            viewEngines.Insert(
                0,
                new ThemeableRazorViewEngine(
                    resolver.GetService<IBuildManager>(),
                    resolver.GetService<IViewPageActivator>())
                    {
                        CurrentTheme = getTheme
                    });
        }
    }
}